/*
 * Copyright (c) 2017 Baidu, Inc. All Rights Reserve. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package org.jflame.uid.worker.dao;

import org.jflame.uid.worker.entity.WorkerNodeEntity;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**
 * DAO for M_WORKER_NODE
 *
 * @author yutianbao
 */
public interface WorkerNodeMapper {

    /**
     * Get {@link WorkerNodeEntity} by node host
     * 
     * @param host
     * @param port
     * @return
     */
    @Select("SELECT id,host_name,port,node_type,launch_date,modified,created FROM worker_node WHERE host_name=#{host} AND port=#{port}")
    @Results(value = { @Result(column = "id", property = "id", id = true),
            @Result(column = "host_name", property = "hostName"),@Result(column = "port", property = "port"),
            @Result(column = "node_type", property = "nodeType"),
            @Result(column = "launch_date", property = "launchDate"),
            @Result(column = "modified", property = "modified"),@Result(column = "created", property = "created") })
    WorkerNodeEntity getWorkerNodeByHostPort(@Param("host") String host, @Param("port") String port);

    /**
     * Add {@link WorkerNodeEntity}
     * 
     * @param workerNodeEntity
     */
    @Insert("INSERT INTO worker_node(host_name,port,node_type,launch_date,modified,created) VALUES(#{hostName},#{port},#{nodeType},#{launchDate},NOW(),NOW())")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int addWorkerNode(WorkerNodeEntity workerNodeEntity);

}
